package com.sam.scanner.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilerecognition.engine.RecogResult;
import com.mobilerecognition.phonenumer.ui.ScanerActivity;
import com.mobilerecognition.phonenumer.utils.Tools;
import com.sam.scanner.R;
import com.sam.scanner.adapter.QuitPackageRecordAdapter;

import java.util.ArrayList;

public class SearchScanerActivity extends ScanerActivity {

    ImageView       mFlashImage;
    LinearLayout    bottomView;
    TextView        tv_toobar_title;
    RelativeLayout  rl_flash;
    boolean         isOpen = false;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RelativeLayout topView = (RelativeLayout) getLayoutInflater().inflate( R.layout.view_activity_top_bar, null, false);
        topView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int topHeight = topView.getMeasuredHeight();
        topView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, topHeight));
        mFlashImage = topView.findViewById(R.id.iv_flash);
        setTopMargin(48);
        addView2Root(topView);
        tv_toobar_title = topView.findViewById(R.id.tv_toobar_title);
        rl_flash = topView.findViewById(R.id.rl_flash);
        tv_toobar_title.setText("查询");

        bottomView = (LinearLayout) getLayoutInflater().inflate(R.layout.view_activity_out_package, null, false);
        bottomView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int bottomHeight = bottomView.getMeasuredHeight();
        bottomView.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, bottomHeight));
        addView2BottomLayout(bottomView);

        bottomView.findViewById(R.id.btn_ok_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText searchDataEt = bottomView.findViewById(R.id.et_search);
                Intent intent = new Intent(getApplicationContext(),SearchContentActivity.class);
                intent.putExtra("searchData",searchDataEt.getText().toString());
                startActivity(intent);
            }
        });
        mFlashImage.setOnClickListener(view -> {
            isOpen = !isOpen;
            openFlash(isOpen);
            mFlashImage.setImageResource(isOpen ? R.drawable.flash_off_y : R.drawable.flash_on_y);
        });
        findViewById(R.id.tv_back).setOnClickListener(view -> finish());

    }

    private int getScreenHeight() {
        DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
        return dm.heightPixels;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        openFlash(false);
    }

    public void setScanText(String text) {
        mQrCodeFinderView.setScanText(text);
    }


    @Override
    protected void handlerRes(RecogResult result, Bitmap bmImage) {
        super.handlerRes(result, bmImage);
        setScanText("订单号: " + lineNum);
        setIsQrCodeOne(true);
        Intent intent = new Intent(getApplicationContext(),OutSearchListActivity.class);
        intent.putExtra("searchData",lineNum);
        startActivity(intent);
    }


    @Override
    protected void outTimeWarning() {

    }

    private void initScaner() {
        initScan();
        mQrCodeFinderView.setScanText("识别订单中...");

    }


}
